package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmProjetDetailTypeService;
import com.mdd.admin.validate.ClientCrmProjetDetailTypeCreateValidate;
import com.mdd.admin.validate.ClientCrmProjetDetailTypeUpdateValidate;
import com.mdd.admin.validate.ClientCrmProjetDetailTypeSearchValidate;
import com.mdd.admin.vo.ClientCrmProjetDetailTypeListedVo;
import com.mdd.admin.vo.ClientCrmProjetDetailTypeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmProjetDetailType;
import com.mdd.common.entity.Declaration;
import com.mdd.common.mapper.ClientCrmProjetDetailTypeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 项目政策关联申报条件实现类
 * @author LikeAdmin
 */
@Service
public class ClientCrmProjetDetailTypeServiceImpl implements IClientCrmProjetDetailTypeService {
        
    @Resource
    ClientCrmProjetDetailTypeMapper clientCrmProjetDetailTypeMapper;

    /**
     * 项目政策关联申报条件列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmProjetDetailTypeListedVo>
     */
    @Override
    public PageResult<ClientCrmProjetDetailTypeListedVo> list(PageValidate pageValidate, ClientCrmProjetDetailTypeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmProjetDetailType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        clientCrmProjetDetailTypeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:projetId@projet_id:int",
            "=:declarationTypeId@declaration_type_id:int",
        });

        IPage<ClientCrmProjetDetailType> iPage = clientCrmProjetDetailTypeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmProjetDetailTypeListedVo> list = new LinkedList<>();
        for(ClientCrmProjetDetailType item : iPage.getRecords()) {
            ClientCrmProjetDetailTypeListedVo vo = new ClientCrmProjetDetailTypeListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 项目政策关联申报条件详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCrmProjetDetailType
     */
    @Override
    public ClientCrmProjetDetailTypeDetailVo detail(Integer id) {
        ClientCrmProjetDetailType model = clientCrmProjetDetailTypeMapper.selectOne(
                new QueryWrapper<ClientCrmProjetDetailType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmProjetDetailTypeDetailVo vo = new ClientCrmProjetDetailTypeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 项目政策关联申报条件新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmProjetDetailTypeCreateValidate createValidate) {
        ClientCrmProjetDetailType model = new ClientCrmProjetDetailType();
        model.setProjetId(createValidate.getProjetId());
        model.setDeclarationTypeId(createValidate.getDeclarationTypeId());
        clientCrmProjetDetailTypeMapper.insert(model);
        // 调用存储过程
        clientCrmProjetDetailTypeMapper.callMatchingProcedure();
    }

    /**
     * 项目政策关联申报条件批量新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void addList(ClientCrmProjetDetailTypeCreateValidate createValidate) {
        if(createValidate.getDeclarationTypeIdList().size()<0||createValidate.getDeclarationTypeIdList()==null){
            return;
        }
        LambdaQueryWrapper<ClientCrmProjetDetailType> deleteQueryWrapper = new LambdaQueryWrapper<>();
        deleteQueryWrapper.eq(StringUtil.isNotEmpty(String.valueOf(createValidate.getProjetId())),ClientCrmProjetDetailType::getProjetId,createValidate.getProjetId());
        clientCrmProjetDetailTypeMapper.delete(deleteQueryWrapper);


        for (String  declarationTypeId: createValidate.getDeclarationTypeIdList()) {
            ClientCrmProjetDetailType model = new ClientCrmProjetDetailType();
            model.setProjetId(createValidate.getProjetId());
            model.setDeclarationTypeId(Integer.valueOf(declarationTypeId));
            clientCrmProjetDetailTypeMapper.insert(model);
        }

        // 调用存储过程
        clientCrmProjetDetailTypeMapper.callMatchingProcedure();
    }

    /**
     * 项目政策关联申报条件编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmProjetDetailTypeUpdateValidate updateValidate) {
        ClientCrmProjetDetailType model = clientCrmProjetDetailTypeMapper.selectOne(
                new QueryWrapper<ClientCrmProjetDetailType>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setProjetId(updateValidate.getProjetId());
        model.setDeclarationTypeId(updateValidate.getDeclarationTypeId());
        clientCrmProjetDetailTypeMapper.updateById(model);
        // 调用存储过程
        clientCrmProjetDetailTypeMapper.callMatchingProcedure();
    }

    /**
     * 项目政策关联申报条件删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmProjetDetailType model = clientCrmProjetDetailTypeMapper.selectOne(
                new QueryWrapper<ClientCrmProjetDetailType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        clientCrmProjetDetailTypeMapper.delete(new QueryWrapper<ClientCrmProjetDetailType>().eq("id", id));
        // 调用存储过程
        clientCrmProjetDetailTypeMapper.callMatchingProcedure();
    }

}
